﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadLibrary.Web
{
    /// <summary>
    /// Holds pointer group of CSS styles.
    /// </summary>
    public sealed class CssGroup : IBuildable, IParsable
    {
        /// <summary>
        /// Gets or sets group name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets group type.
        /// </summary>
        public CssGroupType Type { get; set; }

        /// <summary>
        /// Creates new empty CSS group.
        /// </summary>
        public CssGroup()
        {
            this.Name = "";
            this.Type = CssGroupType.Element;
        }

        /// <summary>
        /// Creates new CSS group with specified name. Defaultly sets group type to element.
        /// </summary>
        /// <param name="Name">Specifies name of new group.</param>
        public CssGroup(string Name)
        {
            this.Name = Name;
            this.Type = CssGroupType.Element;
        }

        /// <summary>
        /// Create new CSS group with specified name and type.
        /// </summary>
        /// <param name="Name">Specifies name of new group.</param>
        /// <param name="Type">Specifies type of new group.</param>
        public CssGroup(string Name, CssGroupType Type)
        {
            this.Name = Name;
            this.Type = Type;
        }

        /// <summary>
        /// Builds current CSS group pointer into string.
        /// </summary>
        /// <returns>String built from current CSS group pointer.</returns>
        string IBuildable.Build()
        {
            string ret = "";
            if(this.Type == CssGroupType.Class)
                ret = ".";
            else if(this.Type == CssGroupType.PseudoClass)
                ret = ":";
            else if(this.Type == CssGroupType.Id)
                ret = "#";
            else if(this.Type == CssGroupType.Element)
                ret = " ";
            return ret + this.Name;
        }

        /// <summary>
        /// Parses specified string into current CSS group.
        /// </summary>
        /// <param name="input">Specifies string with has to be parsed into current CSS group.</param>
        void IParsable.Parse(string input)
        {
            if(input.StartsWith(":"))
            {
                this.Type = CssGroupType.PseudoClass;
                this.Name = input.Remove(0, 1);
            }
            else if(input.StartsWith("."))
            {
                this.Type = CssGroupType.Class;
                this.Name = input.Remove(0, 1);
            }
            else if(input.StartsWith("#"))
            {
                this.Type = CssGroupType.Id;
                this.Name = input.Remove(0, 1);
            }
            else if(input.StartsWith(" "))
            {
                this.Type = CssGroupType.Element;
                this.Name = input.Remove(0, 1);
            }
            else
            {
                this.Type = CssGroupType.Element;
                this.Name = input;
            }
        }
    }
}